package com.xinghou.platform.file.chain.command.photo;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.chain.Command;
import org.apache.commons.chain.Context;

import com.xinghou.platform.file.model.ResourceContext;
import com.xinghou.platform.file.service.UpphotoService;
import com.xinghou.platform.util.AppContextUtil;
import com.xinghou.platform.util.ConfigUtil;
import com.xinghou.platform.util.StringUtils;

public class PhotoDataProcessCommand implements Command{

	private UpphotoService upphotoService;
	
	@Override
	public boolean execute(Context context) throws Exception {
		@SuppressWarnings("unchecked")
		ResourceContext<byte[], String> resourceContext = (ResourceContext<byte[], String>)context;

		
		Map<String, String> result = new HashMap<String, String>();
		result.put("photoname", resourceContext.getSource().getResourceName());
		result.put("pictureurl", resourceContext.getExtars()[0].getSubPath().replace("\\", "/"));
		result.put("userid", resourceContext.getRequest().getUserId());
		upphotoService = (UpphotoService)AppContextUtil.getBean("upphotoService");
		//更新用户头像图片数据
		int pid=0;
		String sharetype = resourceContext.getRequest().getSharetype();
		if(sharetype==null){
			if(StringUtils.isBlank(resourceContext.getRequest().getShareid())){
				pid = upphotoService.updateUserphotoInfoOther(resourceContext.getRequest().getUserId(),result);
			}else{
				result.put("shareid", resourceContext.getRequest().getShareid());
				result.put("sharetype", resourceContext.getRequest().getSharetype());
				pid = upphotoService.updatePhotoShareData(result);
			}
		}else{
			if(sharetype.equals("3") || sharetype.equals("4") || sharetype.equals("5"))
			{
				try{
					result.put("shareid", resourceContext.getRequest().getShareid());
					result.put("sharetype", resourceContext.getRequest().getSharetype());
					pid = upphotoService.updatePhotoShareData(result);
				}catch(Exception e){
					e.printStackTrace();
				}
			}else{
				result.put("shareid", resourceContext.getRequest().getShareid());
				result.put("sharetype", resourceContext.getRequest().getSharetype());
				pid = upphotoService.updatePhotoShareData(result);
			}
		}

		context.put("pid", pid);
		return false;
	}


}
